package com.reyco1.medusa.util
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	
	public class AlignUtil
	{
		public static function topLeft(targetContainer:DisplayObjectContainer, target:DisplayObject, paddingX:Number = 0, paddingY:Number = 0):void
		{
			var dimenstions:Array = getDimensions(targetContainer);
			target.x = 0 + paddingX;
			target.y = 0 + paddingY;
		}
		
		public static function topCenter(targetContainer:DisplayObjectContainer, target:DisplayObject, paddingX:Number = 0, paddingY:Number = 0):void
		{
			var dimenstions:Array = getDimensions(targetContainer);
			target.x = (dimenstions[0] * 0.5 - target.width * 0.5) + paddingX;
			target.y = 0 + paddingY;
		}
		
		public static function topRight(targetContainer:DisplayObjectContainer, target:DisplayObject, paddingX:Number = 0, paddingY:Number = 0):void
		{
			var dimenstions:Array = getDimensions(targetContainer);
			target.x = (dimenstions[0] - target.width) + paddingX;
			target.y = 0 + paddingY;
		}
		
		public static function middleLeft(targetContainer:DisplayObjectContainer, target:DisplayObject, paddingX:Number = 0, paddingY:Number = 0):void
		{
			var dimenstions:Array = getDimensions(targetContainer);
			target.x = 0 + paddingX;
			target.y = (dimenstions[1] * 0.5 - target.height * 0.5) + paddingY;
		}
		
		public static function middleCenter(targetContainer:DisplayObjectContainer, target:DisplayObject, paddingX:Number = 0, paddingY:Number = 0):void
		{
			var dimenstions:Array = getDimensions(targetContainer);
			target.x = (dimenstions[0] * 0.5 - target.width * 0.5) + paddingX;
			target.y = (dimenstions[1] * 0.5 - target.height * 0.5) + paddingY;
		}
		
		public static function middleRight(targetContainer:DisplayObjectContainer, target:DisplayObject, paddingX:Number = 0, paddingY:Number = 0):void
		{
			var dimenstions:Array = getDimensions(targetContainer);
			target.x = (dimenstions[0] - target.width) + paddingX;
			target.y = (dimenstions[1] * 0.5 - target.height * 0.5) + paddingY;
		}
		
		public static function bottomLeft(targetContainer:DisplayObjectContainer, target:DisplayObject, paddingX:Number = 0, paddingY:Number = 0):void
		{
			var dimenstions:Array = getDimensions(targetContainer);
			target.x = 0 + paddingX;
			target.y = (dimenstions[1] - target.height) + paddingY;
		}
		
		public static function bottomCenter(targetContainer:DisplayObjectContainer, target:DisplayObject, paddingX:Number = 0, paddingY:Number = 0):void
		{
			var dimenstions:Array = getDimensions(targetContainer);
			target.x = (dimenstions[0] * 0.5 - target.width * 0.5) + paddingX;
			target.y = (dimenstions[1] - target.height) + paddingY;
		}
		
		public static function bottomRight(targetContainer:DisplayObjectContainer, target:DisplayObject, paddingX:Number = 0, paddingY:Number = 0):void
		{
			var dimenstions:Array = getDimensions(targetContainer);
			target.x = (dimenstions[0] - target.width) + paddingX;
			target.y = (dimenstions[1] - target.height) + paddingY;
		}
		
		/* private */
		
		private static function getDimensions(targetContainer:DisplayObjectContainer):Array
		{
			var targetWidth:Number  = targetContainer is Stage ? Stage(targetContainer).stageWidth  : targetContainer.width;
			var targetHeight:Number = targetContainer is Stage ? Stage(targetContainer).stageHeight : targetContainer.height;
			
			return [targetWidth, targetHeight];
		}
	}
}